package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.itheima.CheckItemService;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*
* 检查项管理*/
@Service(interfaceClass=CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
   //新增检查项
   @Autowired
   private CheckItemDao checkItemDao;

    public void add(CheckItem checkItem) {
       checkItemDao.add(checkItem);

    }
    //新增检查分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //分页助手完成分页会更简单

        PageHelper.startPage(currentPage, pageSize);

        com.github.pagehelper.Page<CheckItem> page = checkItemDao.selectByCondition(queryString);


        long total = page.getTotal();

        List<CheckItem> rows = page.getResult();



        return new PageResult(total,rows);
    }

    //服务层检查项删除id
    @Override
    public void deleteById(Integer id) {
        //判断当前的检查项是否关联检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count>0){
            //当前的检查项关联检查组,不能删除
            new RuntimeException();
        }
        checkItemDao.deleteById(id);

    }
    //编辑检查项
    @Override
    public void edit(CheckItem checkItem) {

        checkItemDao.edit(checkItem);
    }
    //编辑检查项回写
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
    //检查组查询所有
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
